import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    meta: { title: 'admin后台管理' },
    component: () => import('@/pages/Layout.vue'),
    children: [
      {//重定向到首页
        path: '/',
        redirect: '/admin'
      },
      {//首页
        path: '/admin',
        name: 'Admin',
        meta: { title: 'admin首页' },
        component: () => import('@/layout/Index.vue')
      },
      {//菜单
        path: '/menu',
        name: 'Menu',
        meta: { title: 'admin菜单' },
        component: () => import('@/layout/system/menu/Index.vue'),
        children: [

          {//菜单添加
            path: '/menu/add',
            name: 'MenuAdd',
            meta: { title: 'admin菜单添加' },
            component: () => import('@/layout/system/menu/Menuadd.vue'),
          },
          {//菜单编辑
            path: '/menu/edit/:id?',
            name: 'MenuEdit',
            meta: { title: 'admin菜单编辑' },
            component: () => import('@/layout/system/menu/Menuadd.vue'),
          }
        ]
      },
      {//角色
        path: '/role',
        name: 'Role',
        meta: { title: 'admin角色管理' },
        component: () => import('@/layout/system/role/Index.vue'),
        children: [
          {//角色添加
            path: '/role/add',
            name: 'RoleAdd',
            meta: { title: 'admin角色添加' },
            component: () => import('@/layout/system/role/Roleadd.vue'),
          },
          {//角色编辑
            path: '/role/edit/:id?',
            name: 'RoleEdit',
            meta: { title: 'admin角色编辑' },
            component: () => import('@/layout/system/role/Roleadd.vue'),
          }
        ]
      },
      {//管理员
        path: '/adminstors',
        name: 'Adminstors',
        meta: { title: 'admin管理员管理' },
        component: () => import('@/layout/system/adminstors/Index.vue'),
        children: [
          {//管理员添加
            path: '/adminstors/add',
            name: 'AdminstorsAdd',
            meta: { title: 'admin管理员添加' },
            component: () => import('@/layout/system/adminstors/Adminstorsadd.vue'),
          },
          {//管理员编辑
            path: '/adminstors/edit/:id?',
            name: 'AdminstorsEdit',
            meta: { title: 'admin管理员编辑' },
            component: () => import('@/layout/system/adminstors/Adminstorsadd.vue'),
          }
        ]
      },
      {//订单
        path: '/dingdan',
        name: 'Dingdan',
        meta: { title: 'admin订单管理' },
        component: () => import('@/layout/shop/dingdan/Index.vue'),
        children: [
          {//订单添加
            path: '/dingdan/add',
            name: 'DingdanAdd',
            meta: { title: 'admin订单添加' },
            component: () => import('@/layout/shop/dingdan/Dingdanadd.vue'),
          },
          {//订单编辑
            path: '/dingdan/edit',
            name: 'DingdanEdit',
            meta: { title: 'admin订单编辑' },
            component: () => import('@/layout/shop/dingdan/Dingdanedit.vue'),
          }
        ]
      },
      {//管理
        path: '/guanli',
        name: 'Guanli',
        meta: { title: 'admin管理' },
        component: () => import('@/layout/shop/guanli/Index.vue'),
        children: [
          {//管理添加
            path: '/guanli/add',
            name: 'GuanliAdd',
            meta: { title: 'admin管理添加' },
            component: () => import('@/layout/shop/guanli/Guanliadd.vue'),
          },
          {//管理编辑
            path: '/guanli/edit/:id',
            name: 'GuanliEdit',
            meta: { title: 'admin管理编辑' },
            component: () => import('@/layout/shop/guanli/Guanliadd.vue'),
          }
        ]
      },
      {//会员
        path: '/huiyuan',
        name: 'Huiyuan',
        meta: { title: 'admin会员管理' },
        component: () => import('@/layout/shop/huiyuan/Index.vue'),
        children: [
          {//管理添加
            path: '/huiyuan/add',
            name: 'HuiyuanAdd',
            meta: { title: 'admin会员添加' },
            component: () => import('@/layout/shop/huiyuan/Huiyuanadd.vue'),
          },
          {//管理编辑
            path: '/huiyuan/edit/:id',
            name: 'HuiyuanEdit',
            meta: { title: 'admin会员编辑' },
            component: () => import('@/layout/shop/huiyuan/Huiyuanadd.vue'),
          }
        ]
      },
      {//活动
        path: '/huodong',
        name: 'Huodong',
        meta: { title: 'admin活动管理' },
        component: () => import('@/layout/shop/huodong/Index.vue'),
        children: [
          {//活动添加
            path: '/huodong/add',
            name: 'HuodongAdd',
            meta: { title: 'admin活动添加' },
            component: () => import('@/layout/shop/huodong/Huodongadd.vue'),
          },
          {//活动编辑
            path: '/huodong/edit/:id',
            name: 'HuodongEdit',
            meta: { title: 'admin活动编辑' },
            component: () => import('@/layout/shop/huodong/Huodongadd.vue'),
          }
        ]
      },
      {//秒杀
        path: '/miaosha',
        name: 'Miaosha',
        meta: { title: 'admin秒杀管理' },
        component: () => import('@/layout/shop/miaosha/Index.vue'),
        children: [
          {//秒杀添加
            path: '/miaosha/add',
            name: 'MiaoshaAdd',
            meta: { title: 'admin秒杀添加' },
            component: () => import('@/layout/shop/miaosha/Miaoshaadd.vue'),
          },
          {//秒杀编辑
            path: '/miaosha/edit/:id',
            name: 'MiaoshaEdit',
            meta: { title: 'admin秒杀编辑' },
            component: () => import('@/layout/shop/miaosha/Miaoshaadd.vue'),
          }
        ]
      },
      {//分类
        path: '/shoplist',
        name: 'Shoplist',
        meta: { title: 'admin分类管理' },
        component: () => import('@/layout/shop/shoplist/Index.vue'),
        children: [
          {//分类添加
            path: '/shoplist/add',
            name: 'ShoplistAdd',
            meta: { title: 'admin分类添加' },
            component: () => import('@/layout/shop/shoplist/Shoplistadd.vue'),
          },
          {//分类编辑
            path: '/shoplist/edit/:id',
            name: 'ShoplistEdit',
            meta: { title: 'admin分类编辑' },
            component: () => import('@/layout/shop/shoplist/Shoplistadd.vue'),
          }
        ]
      },
      {//规格
        path: '/shoptions',
        name: 'Shoptions',
        meta: { title: 'admin规格管理' },
        component: () => import('@/layout/shop/shoptions/Index.vue'),
        children: [
          {//规格添加
            path: '/shoptions/add',
            name: 'ShoptionsAdd',
            meta: { title: 'admin规格添加' },
            component: () => import('@/layout/shop/shoptions/Shoptionsadd.vue'),
          },
          {//规格编辑
            path: '/shoptions/edit/:id',
            name: 'ShoptionsEdit',
            meta: { title: 'admin规格编辑' },
            component: () => import('@/layout/shop/shoptions/Shoptionsadd.vue'),
          }
        ]
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    meta: { title: 'admin登录' },
    component: () => import('@/pages/Login.vue')
  },
  {
    path: '*',
    name: '404',
    meta: { title: '404' },
    component: () => import('@/pages/Nodfound.vue')
  }
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {

  // console.log(store.getters.isLogin);
  // 判断是否登录
  if (store.getters.isLogin) {//已登录
    if (to.path == '/login' || to.path == '/' || to.path == '/admin') {//去登录页放行
      next()
    } else {//非登录页
      // if()
      let arr = store.getters.getMenus_url
      let rst = arr.some(item => {
        return item == to.path || to.path.startsWith(item)
      })
      if (rst) {//访问白名单路由
        next()
        document.title = to.meta.title//设置标头
      } else {//访问非白名单路由
        // alert('您未有该权限进入此页面')
        next('/login')
      }
    }

  } else {//未登录
    if (to.path == '/login') {//如果访问登录页面  放行
      next()
    } else { //访问的不是登录页  强制跳转
      next('/login')
    }
  }
  // next()
})
export default router
